using GameKit.Core.Context;

namespace OneGame.Gameplay
{
    public class StateController : ISystem
    {
        // PUBLIC
        public IContext Context { get; set; }

        // PRIVATE
        private readonly AttributeWatcher _healthWatcher;
        private readonly IStateView _stateView;
        private int _maxHealth;

        public StateController(IStateView stateView)
        {
            _healthWatcher = new AttributeWatcher();
            _stateView = stateView;
        }

        public void OnInitialize()
        {
        }

        public void OnUpdate(float deltaTime)
        {
        }

        public void OnDestroy()
        {
        }

        public void Init(int maxHealth)
        {
            _maxHealth = maxHealth;
            _healthWatcher.Init(maxHealth, maxHealth);
            _healthWatcher.OnValueChanged += OnHealthChanged;
            Hide();
        }

        public void Hide()
        {
            _stateView.HideState();
        }

        private void OnHealthChanged(int value, int difference)
        {
            _stateView.SetHealth(value, _maxHealth, difference);
        }

        public void TakeDamage(int damage)
        {
            var value = _healthWatcher.CurrentValue - damage;
            _healthWatcher.CurrentValue = value <= 0 ? 0 : value;
        }
    }
}